var e = require("../../../@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.form = function () {
  return function () {
    var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    e.properties || (e.properties = {}), e.properties.formName = {
      type: String,
      value: ""
    }, e.lifetimes || (e.lifetimes = {}), e.methods || (e.methods = {}), e.methods.getValues = function () {
      return r.getValues(this.data.formName)
    }, e.methods.setValues = function (e) {
      return r.setValues(this.data.formName, e)
    }, e.methods.verify = function () {
      var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {},
        t = r.getValues(this.data.formName);
      for (var n in e)
        if (Object.prototype.hasOwnProperty.call(e, n))
          for (var i = e[n], o = t[n], s = 0; s < i.length; s++) {
            var u = i[s];
            for (var f in u)
              if (Object.prototype.hasOwnProperty.call(u, f) && a.verify[f]) {
                var l = u[f];
                if (a.verify[f](l, o)) return $.msg(u.message), Promise.reject(new Error(u.message))
              }
          }
      return t
    };
    var t = e.lifetimes,
      o = t.attached,
      s = t.detached;
    return e.lifetimes.attached = function () {
      if (!this.data.formName) {
        var e = n();
        e.formName = i(), this.data.formName = e.formName
      }
      for (var t = arguments.length, a = new Array(t), r = 0; r < t; r++) a[r] = arguments[r];
      o && o.call.apply(o, [this].concat(a))
    }, e.lifetimes.detached = function () {
      for (var e = arguments.length, t = new Array(e), a = 0; a < e; a++) t[a] = arguments[a];
      s && s.call.apply(s, [this].concat(t))
    }, e
  }
}, exports.formItem = function () {
  return function (e) {
    e.properties || (e.properties = {}), e.properties.name = {
      type: String,
      value: ""
    }, e.properties.formName = {
      type: String,
      value: ""
    }, e.properties.customValue = {
      type: null,
      value: null
    }, e.data || (e.data = {}), e.lifetimes || (e.lifetimes = {}), e.methods || (e.methods = {}), e.observers || (e.observers = {}), e.observers.customValue = function () {
      this.data.customValue && this.data.value !== this.data.customValue && this.setData({
        value: this.data.customValue
      })
    }, e.methods.getValues = function () {
      var e = this.data.formName || n().formName;
      return r.getValues(e)
    }, e.methods.onChange = function (e) {
      var t = this.data.formName || n().formName,
        a = this.data.name;
      this.triggerEvent("change", {
        value: e
      }), t && a && r.onChange(t, a, e)
    };
    var a = e.lifetimes,
      i = a.attached,
      o = a.detached;
    return e.lifetimes.attached = function () {
      var e = this,
        a = this.data.formName || n().formName,
        o = this.data.name;
      if (a && o) {
        var s = r.getValues(a)[o];
        void 0 !== s && this.data.value !== s && this.setData({
          value: s
        }), this.unSubscribe = r.subscribe(a, (function (a) {
          e.onFormValueChange && e.onFormValueChange((0, t.default)(a));
          var r = a[o];
          void 0 !== r && e.data.value !== r && e.setData({
            value: r
          })
        }))
      }
      for (var u = arguments.length, f = new Array(u), l = 0; l < u; l++) f[l] = arguments[l];
      i && i.call.apply(i, [this].concat(f))
    }, e.lifetimes.detached = function () {
      for (var e = arguments.length, t = new Array(e), a = 0; a < e; a++) t[a] = arguments[a];
      this.unSubscribe && this.unSubscribe(), o && o.call.apply(o, [this].concat(t))
    }, e
  }
};
var t = e(require("../../../utils/cloneDeep/index")),
  a = require("./verify");
var r = function () {
  var e = {},
    a = {};

  function r(e) {
    return a[e] || (a[e] = []), a[e]
  }

  function n(t) {
    return e[t] || (e[t] = {}), e[t]
  }
  return {
    onChange: function (e, t, a) {
      n(e)[t] = a;
      var i = n(e);
      r(e).forEach((function (e) {
        return e(i)
      }))
    },
    subscribe: function (e, t) {
      return r(e).push(t),
        function () {
          var a = r(e).indexOf(t);
          r(e).splice(a, 1)
        }
    },
    getValues: function (e) {
      return (0, t.default)(n(e))
    },
    setValues: function (e) {
      var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
      Object.assign(n(e), t);
      var a = n(e);
      r(e).forEach((function (e) {
        return e(a)
      }))
    }
  }
}();

function n() {
  var e = getCurrentPages();
  return e[e.length - 1] || {}
}

function i() {
  return "formName-".concat(Math.random().toString(36).slice(2))
}